package com.yanqu.road.server.manager.activity.jiuzhou;

import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.bussiness.servercenter.ActivityBussiness;
import com.yanqu.road.logic.helper.ActivityHelper;
import com.yanqu.road.server.CrossBaseServer;
import com.yanqu.road.server.TempCrossMgr;
import com.yanqu.road.server.manager.activity.ActivityMgr;
import com.yanqu.road.server.manager.activity.jiuzhou.activity.CrossJiuZhouActivity;
import com.yanqu.road.utils.date.DateHelper;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class CrossJiuZhouMgr extends TempCrossMgr {

    public final static Map<Integer, CrossJiuZhouActivity> activityMap = new ConcurrentHashMap<>();

    public static synchronized CrossJiuZhouActivity getActivity(int activityId) {
        if (!activityMap.containsKey(activityId)) {
            // 读配置 获取活动信息 保证有活动信息
            ActivityInfo activityInfo = ActivityMgr.getActivityInfoMayLoadDB(activityId);
            if (activityInfo == null) {
                return null;
            }
            // 不在展示期也不处理了
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                return null;
            }
            CrossJiuZhouActivity activity = new CrossJiuZhouActivity(activityInfo);
            activityMap.put(activityId, activity);
        }
        return activityMap.get(activityId);
    }

    public static Map<Integer, CrossJiuZhouActivity> getActivityMap() {
        return activityMap;
    }

    @Override
    public boolean save() {
        if (activityMap.size() == 0) {
            return true;
        }
        getLogger().info("jiuzhou save start");
        for (CrossJiuZhouActivity activity : activityMap.values()) {
            activity.save();
        }
        getLogger().info("jiuzhou save end");
        return true;
    }

    @Override
    public boolean removeExpireData() throws Exception {
        List<Integer> removeList = new ArrayList<>();
        long nowTime = System.currentTimeMillis();
        for (int activityId : activityMap.keySet()) {
            CrossJiuZhouActivity activity = activityMap.get(activityId);
            if (activity.getConfig().getActivityInfo() == null || (nowTime > activity.getConfig().getActivityInfo().getEndShowTime() * 1000 + 3 * DateHelper.DAY_MILLIONS)) {
                removeList.add(activityId);
            }
        }
        for (int activityId : removeList) {
            activityMap.remove(activityId);
            getLogger().debug("remove activity {}", activityId);
        }
        return true;
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        getLogger().info("jiuzhou reload config begin...");
        for (CrossJiuZhouActivity activity : activityMap.values()) {
            ActivityInfo activityInfo = ActivityBussiness.getActivityInfoByActivityId(activity.getConfig().getActivityId());
            if (activityInfo != null) {
                activity.reloadConfig(activityInfo);
                getLogger().info("jiuzhou reload config activity {}", activity.getConfig().getActivityInfo().getActivityId());
            }
        }
        // 重新加载数据
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {
        // 获取活动
        String nowTimeStr = DateHelper.getCurrentDateTimeString();
        reloadActivity(nowTimeStr);
        return true;
    }

    /**
     * 提前加载活动
     */
    public static void advanceLoadActivityTask() {
        // 提前一小时加载活动
        long time = System.currentTimeMillis() + DateHelper.HOUR_MILLIONS;
        String timeString = DateHelper.getCurrentDateTimeString(time);
        reloadActivity(timeString);
    }

    public static void reloadActivity(String nowTimeStr) {
        List<Integer> activityTypeList = new ArrayList<>();
        activityTypeList.add(eActivityType.JiuZhou.getValue());
        Map<Integer, ActivityInfo> activityInfoMap = ActivityBussiness.getOpenCrossRankActivityInfoMapByCrossServerId(activityTypeList, nowTimeStr, CrossBaseServer.getInstance().getServerId());
        // 重新加载数据
        for (ActivityInfo activityInfo : activityInfoMap.values()) {
            if (!activityMap.containsKey(activityInfo.getActivityId())) {
                getActivity(activityInfo.getActivityId());
            }
        }
    }

    @Override
    public boolean stop() {
        return true;
    }

    /**
     * 修bug用
     */
    public void fixBug() {
        for (CrossJiuZhouActivity activity : activityMap.values()) {
            ActivityInfo activityInfo = activity.getConfig().getActivityInfo();
            if (!ActivityHelper.activityInShowTime(activityInfo)) {
                continue;
            }
            activity.fixBug("");
        }
    }
}
